package tlsb;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import offline.CGA;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Pipeline;

import util.IFileReader;
import util.IPartition;
import util.RedisCluster;

public class LoadDataToRedis {

	private static Jedis jedis, jedis0, jedis1, jedis2, jedis3, jedis4, jedis5;
	static JedisPool pool, pool0, pool1, pool2, pool3, pool4, pool5;
	static Map<Integer, Jedis> jedisMap = new HashMap<Integer, Jedis>();
	public static final String[] HOSTS = { "10.11.1.57", "10.11.1.60",
			"10.11.1.61", "10.11.1.63", "10.11.1.65", "10.11.1.66" };
	// public static final String[] HOSTS = { "127.0.0.1" };
	public static final int PORT = 6379;

	private static RedisCluster cluster = null;
	private static IPartition part = null;

	public static void init() throws DBException {
		// TODO check llsequence initialization

		String host = "127.0.0.1";
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxActive(100);
		config.setMaxIdle(20);
		config.setMaxWait(1000l);
		//
		cluster = new RedisCluster(HOSTS, PORT);
		part = cluster.getPartition();
		pool = new JedisPool(config, host, PORT, 100 * 1000);
		pool0 = new JedisPool(config, HOSTS[0], PORT, 100 * 1000);
		pool1 = new JedisPool(config, HOSTS[1], PORT, 100 * 1000);
		pool2 = new JedisPool(config, HOSTS[2], PORT, 100 * 1000);
		pool3 = new JedisPool(config, HOSTS[3], PORT, 100 * 1000);
		pool4 = new JedisPool(config, HOSTS[4], PORT, 100 * 1000);
		pool5 = new JedisPool(config, HOSTS[5], PORT, 100 * 1000);

		jedis = pool.getResource();
		jedis0 = pool0.getResource();
		jedis1 = pool1.getResource();
		jedis2 = pool2.getResource();
		jedis3 = pool3.getResource();
		jedis4 = pool4.getResource();
		jedis5 = pool5.getResource();

		jedisMap.put(0, jedis0);
		jedisMap.put(1, jedis1);
		jedisMap.put(2, jedis2);
		jedisMap.put(3, jedis3);
		jedisMap.put(4, jedis4);
		jedisMap.put(5, jedis5);
	}

	public static void cleanup() throws DBException {
		pool.returnResource(jedis);
		pool0.returnResource(jedis0);
		pool1.returnResource(jedis1);
		pool2.returnResource(jedis2);
		pool3.returnResource(jedis3);
		pool4.returnResource(jedis4);
		pool5.returnResource(jedis5);
	}

	public static String extractUid(String uidStr) {
		StringBuffer uid = new StringBuffer();
		for (int i = 0; i < uidStr.length(); i++) {
			if (Character.isDigit(uidStr.charAt(i))) {
				uid.append(uidStr.charAt(i));
			}
		}
		return uid.toString();
	}

	public static void main(String[] args) throws DBException {
		init();
		Utils.loadMap();
		for (int i = 0; i < 6; i++) {
			IFileReader ifr = new IFileReader(new File(String.format("n%d.txt",
					i)));
			ifr.open();

			String line = null;
			HashMap<Integer, Pipeline> pipelines = new HashMap<Integer, Pipeline>();

			int count = 0;
			while ((line = ifr.readLine()) != null) {
				String[] fields = line.split("\t");
				int nid = part.shard(Utils
						.getCidOfSid(extractUid(fields[1])));

				if (pipelines.get(nid) == null) {
					pipelines.put(nid, jedisMap.get(nid).pipelined());
				}

				pipelines.get(nid).sadd(fields[1], fields[2]);

				if ((++count) % 200 == 0) {
					for (Pipeline pipeline : pipelines.values()) {
						pipeline.sync();
					}
				}
			}
			for (Pipeline pipeline : pipelines.values()) {
				pipeline.sync();
			}
			ifr.close();
		}
		cleanup();
	}
}
